package
{
	import ActionButtons.BuyAttacker;
	import ActionButtons.BuyGas;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

	public class HUD
	{
		public var display:Sprite = new Sprite();
		public var playerHomeBase:HomeBase;
		
		private var m_playerInfoTF:TextField;
		private var m_updateTimer:Timer = new Timer(500);
		
		private var m_actionButtons:Vector.<IActionButton> = new Vector.<IActionButton>;
		private var m_actionBar:Sprite = new Sprite();
		
		private var m_displayToButton:Dictionary = new Dictionary();
		
		public function HUD(hb:HomeBase)
		{
			this.playerHomeBase = hb;
			m_updateTimer.addEventListener(TimerEvent.TIMER, onUpdate);
			var playerInfoHud:Sprite = VTUtil.getRoundedRect(100,50,0xffffff,.8);
			m_playerInfoTF = new TextField();
			playerInfoHud.addChild(m_playerInfoTF);
			
			var buyGas:BuyGas = new BuyGas();
			buyGas.homeBase = this.playerHomeBase;
			buyGas.display.addEventListener(MouseEvent.CLICK, onActionClick);
			m_displayToButton[buyGas.display] = buyGas;
			m_actionBar.addChild(buyGas.display);
			
			var buyAttacker:BuyAttacker = new BuyAttacker();
			buyAttacker.homeBase = this.playerHomeBase;
			buyAttacker.display.addEventListener(MouseEvent.CLICK, onActionClick);
			buyAttacker.display.x = m_actionBar.width + 2;
			m_displayToButton[buyAttacker.display] = buyAttacker;
			m_actionBar.addChild(buyAttacker.display);
			
			display.addChild(m_actionBar);
			display.addChild(playerInfoHud);
		}
	
		private function onActionClick(me:MouseEvent):void{
			var actionButton:IActionButton = m_displayToButton[me.currentTarget] as IActionButton;
			if(actionButton.canDoAction()){
				actionButton.doAction();
			}
		}		
		
		public function startUpdates():void{
			m_updateTimer.start();
			m_actionBar.x = display.parent.width/2 - m_actionBar.width/2;
			m_actionBar.y = display.parent.height - m_actionBar.height;			
		}
		
		public function stopUpdates():void{
			m_updateTimer.stop();
		}
		
		private function onUpdate(te:TimerEvent):void{
	
			
			var info:String = '';
			info += 'Health: ' + playerHomeBase.getHealth() + '\n';
			info += 'Gas: ' + playerHomeBase.gasGathered + '\n';
			m_playerInfoTF.text = info;
			m_playerInfoTF.autoSize = TextFieldAutoSize.LEFT;
		}
	}
}